package com.sddz.pacs.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.SysStation;
import com.sddz.pacs.service.SysStationService;
import com.sddz.pacs.support.ResultConstant;
import com.sddz.pacs.util.ShiroUtils;
import com.sddz.pacs.util.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sysStation")
@Api(description="站点管理")
public class SysStationController {
    @Autowired
    private SysStationService sysStationService;


    @PostMapping("/list")
    @SysLogger(value = "查询站点列表",type = "站点管理")
    @ApiOperation(value="获取站点列表", notes="获取站点分页立标")
    @ApiImplicitParam(name = "pageModelParams",value = "分页参数json对象",required = true)
    public JsonResult list(@RequestBody PageModelParams pageModelParams){
        PageInfo<SysStation> pageInfo = sysStationService.selectListByCondition(pageModelParams);
        return JsonResult.success(pageInfo,ResultConstant.STATION_LIST_SUCCESS);
    }
    @PostMapping("/add")
    @SysLogger(value = "新增系统站点",type = "站点管理")
    @ApiOperation(value="新增系统站点")
    @ApiImplicitParam(name = "sysStation",value = "站点json对象",required = true)
    public JsonResult add(@RequestBody SysStation sysStation){
        String stationName=sysStation.getStationName();
        sysStation.setCreateDate(new Date());
            //判断站点是否存在
            Integer count = sysStationService.queryStationByStationNames(stationName);
            if (count > 0) {
                return JsonResult.failure("站点名重复已存在");
            }
            //存储站点
            boolean flag = sysStationService.insert(sysStation);
            if (!flag) {
                return JsonResult.failure(ResultConstant.ADD_FAILURE);
            }
            return JsonResult.success(ResultConstant.ADD_SUCCESS);
    }



    @GetMapping("/delete/{id}")
    @SysLogger(value = "删除系统站点",type = "站点管理")
    @ApiOperation(value="删除系统站点")
    @ApiImplicitParam(name = "id",value = "用户唯一标识",required = true)
    public JsonResult delete(@PathVariable("id") String id){
        boolean flag = sysStationService.deleteById(id);
        if(!flag){
            return JsonResult.failure(ResultConstant.DELETE_FAILURE);
        }
        return JsonResult.success(ResultConstant.DELETE_SUCCESS);
    }
    @PostMapping("/update")
    @SysLogger(value = "更新系统站点",type = "站点管理")
    @ApiOperation(value="更新系统站点")
    @ApiImplicitParam(name = "sysStation",value = "站点json对象",required = true)
    public JsonResult update(@RequestBody SysStation station){
        //1.校验数据完整性
        ValidatorUtils.validateEntity(station);
        station.setUpdateDate(new Date());
        station.setUpdateId(ShiroUtils.getUserId());
        boolean flag = sysStationService.updateById(station);
        if(!flag){
            return JsonResult.failure(ResultConstant.UPDATE_FAILURE);
        }
        return JsonResult.success(ResultConstant.UPDATE_SUCCESS);
    }
    @GetMapping("/detail/{id}")
    @SysLogger(value = "查看系统站点详情",type = "查看系统站点详情站点日志")
    @ApiOperation(value="查看系统站点详情")
    @ApiImplicitParam(name = "id",value = "用户唯一标识",required = true)
    public JsonResult detaile(@PathVariable("id") String id){
        SysStation station = sysStationService.selectById(id);

        return JsonResult.success(station,ResultConstant.STATION_DETAILS_SUCCESS);
    }
    //桥西区的树形结构
    @PostMapping("/treeList")
    @ApiOperation(value = "获取桥西区的树形结构")
    @ApiImplicitParam()
    public JsonResult list() {
        EntityWrapper<SysStation> ew = new EntityWrapper<>();
        List<SysStation> list = sysStationService.selectList(ew);
        return JsonResult.success(list, ResultConstant.GET_TREE_VIEW_SUCCESS);
    }

}
